//
//  PickupMoneyViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/8.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "PickupMoneyViewController.h"
#import "ChooseCountViewController.h"
#import "ZSDPaymentForm.h"
#import "ZSDPaymentView.h"

@interface PickupMoneyViewController ()
{
    NSString *_cardId;
    NSString *_type;
    NSString *_rechage;
}
@property (weak, nonatomic) IBOutlet UILabel *MoenyLabel;
@property (weak, nonatomic) IBOutlet UIImageView *IconImg;
@property (weak, nonatomic) IBOutlet UILabel *CountType;
@property (weak, nonatomic) IBOutlet UILabel *CountLabel;
@property (weak, nonatomic) IBOutlet UITextField *MoneyTF;
@property (weak, nonatomic) IBOutlet UILabel *FitLabel;
@end

@implementation PickupMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"提现";
    _MoneyTF.keyboardType = UIKeyboardTypeDecimalPad;
    self.view.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [self downloadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPassword:) name:@"GetPassword" object:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_MoneyTF resignFirstResponder];
}

- (IBAction)ChooseCountClick:(UIButton *)sender {
    ChooseCountViewController *choose = [[ChooseCountViewController alloc] init];
    choose.back = ^(NSDictionary *di) {
        NSString *str;
        if ([di[@"accType"]integerValue]==1) {
            str = @"支付宝";
            _IconImg.image = [UIImage imageNamed:@"alipay"];
        }else if([di[@"accType"]integerValue]==2){
            str = @"微信";
            _IconImg.image = [UIImage imageNamed:@"wechatpay"];
        }else{
            str = @"银行卡";
        }
        _CountType.text = str;
        NSString *num = di[@"accNo"];
        _CountLabel.text = num;
        _cardId = di[@"id"];
        _type = di[@"accType"];
    };
    [self.navigationController pushViewController:choose animated:YES];
}

- (void)downloadData
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MinePickUpMoney) Parameters:@{@"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([String(responseObject[@"status_code"]) isEqualToString:@"1008"]) {
            [self.view Message:responseObject[@"msg"]];
        }else{
            _MoenyLabel.text = [NSString stringWithFormat:@"%@", responseObject[@"data"][@"useraward"]];
            [_MoneyTF.rac_textSignal subscribeNext:^(NSNumber *result) {
                if(![TXUtilsString isBlankString:String(result)]){
                    _rechage = [NSString stringWithFormat:@"%.2f", [_MoneyTF.text floatValue]*[responseObject[@"data"][@"charge"] floatValue]];
                    _FitLabel.text = [NSString stringWithFormat:@"手续费：%@", _rechage];
                }
            }];
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (IBAction)SubmitClick:(UIButton *)sender {
    if (!_cardId) {
        [self.view Message:@"请选择提现账户"];
        return;
    }
    if (_MoneyTF.text.length == 0) {
        [self.view Message:@"请输入金额"];
        return;
    }
    [self showKeyboard];
}

- (void)showKeyboard
{
    ZSDPaymentView *payment = [[ZSDPaymentView alloc]init];
    payment.title = @"请输入支付密码";
    payment.goodsName = @"零钱";
    payment.amount = [_MoneyTF.text floatValue];
    [payment show];
}

- (void)getPassword:(NSNotification *)notice{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MinePickUpClick) Parameters:@{@"userToken":[Utils getUserToken], @"money":_MoneyTF.text, @"accId":_cardId, @"payPwd":notice.object, @"charge": _rechage, @"accType":_type} Success:^(id responseObject) {
        [self.view removeAnyView];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dismiss" object:nil];
        if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
            [self.view Message:responseObject[@"msg"]];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
